//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Mapping.Properties;
using EntLibContrib.Mapping.Configuration;

namespace EntLibContrib.Mapping.Maps
{
	/// <summary>
	/// Map to hold the domain object property name and DataSet table column name of a single mapping
	/// </summary>
	[ConfigurationElementType(typeof(MapData))]
	public class Map : IMap
	{
		#region Fields
		private string name;
		private string column;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the name of the map.
		/// </summary>
		/// <value>The name of the map.</value>
		/// <remarks>Both fields and properties can be mapped.
		/// All names are case sesitive</remarks>
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		/// <summary>
		/// Gets or sets the column name of a table in a dataset.
		/// </summary>
		/// <value>The column name.</value>
		/// <remarks>This is the name given to a table column within a DataSet,
		/// where the DataSet type is acting as a Data Transfer Object.</remarks>
		public string ColumnName
		{
			get { return column; }
			set { column = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="Map"/> class.
		/// </summary>
		/// <param name="name">The map name.</param>
		public Map(string name)
			: this(name, name)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Map"/> class.
		/// </summary>
		/// <param name="name">The map name.</param>
		/// <param name="columnName">The name of the DataSet table column.</param>
		public Map(string name, string columnName)
		{
			ValidateAndStoreParameters(name, columnName);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Validates and stores the parameters.
		/// </summary>
		/// <param name="mapName">Name of the map.</param>
		/// <param name="columnName">The DataSet table column name.</param>
		private void ValidateAndStoreParameters(string mapName, string columnName)
		{
			// check for a null or empty name
			if (string.IsNullOrEmpty(mapName))
				throw new ArgumentNullException("mapName", InternalResources.ExceptionMapNameNullOrEmpty);
			// store the name
			this.name = mapName;
			// store the column name
			this.column = columnName;
		}
		#endregion
	}
}
